package com.etc.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.entity.Housing;
import com.etc.entity.Users;
import com.etc.service.HousingService;
import com.etc.service.PersonService;
import com.etc.service.impl.HousingServiceImpl;
import com.etc.service.impl.PerviceServiceImpl;

/**
 * Servlet implementation class ShowServlet
 */
@WebServlet("/ss.do")
public class ShowServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private HousingService hService = new HousingServiceImpl();
	PersonService pService = new PerviceServiceImpl();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String op = "apartment01";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if (op.equals("apartment01")) {
			doQuery(request, response,"蔡塘悦舍","apartment01.jsp");
		}else if (op.equals("apartment02")) {
			doQuery(request, response,"悦舍大院","apartment02.jsp");
		}
		else if (op.equals("apartment03")) {
			doQuery(request, response,"悦舍阔居","apartment03.jsp");
		}
		
		
	}

	private void doQuery(HttpServletRequest request, HttpServletResponse response,String hsName,String url)
			throws UnsupportedEncodingException, ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		if(request.getSession().getAttribute("username")!=null) {
			String sess=request.getSession().getAttribute("username").toString();
			System.out.println("session : "+sess);
			//转发用户的用户昵称和头像使他能够被调用
			String personImg = pService.SelPersonAll(sess).get(0).getPersonImg();
			String personNickName = pService.SelPersonAll(sess).get(0).getPersonNickName();
			request.setAttribute("personImg",personImg );
			request.setAttribute("personNickName",personNickName );
		}
		
		
		List<Housing> housinglist = hService.queryHsByName(hsName);
		//3传递数据给jsp可以使用内置对象request,对象原型是HttpServletRequest
		request.setAttribute("housinglist", housinglist);
		//4跳转到jsp页面去？ 转发
//		request.getRequestDispatcher("url").forward(request, response);
		request.getRequestDispatcher(url).forward(request, response);
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
